package cn.guetzjb.sys.service.impl;

import cn.guetzjb.core.exception.ServiceException;
import cn.guetzjb.security.utils.SecurityUtils;
import cn.guetzjb.sys.domain.SysMenu;
import cn.guetzjb.sys.domain.SysRole;
import cn.guetzjb.sys.domain.SysRoleMenu;
import cn.guetzjb.sys.mapper.SysRoleMapper;
import cn.guetzjb.sys.model.LoginUser;
import cn.guetzjb.sys.service.SysMenuService;
import cn.guetzjb.sys.mapper.SysMenuMapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author 13080
 * @description 针对表【sys_menu(菜单权限表)】的数据库操作Service实现
 * @createDate 2024-01-03 22:23:01
 */
@Service
@RequiredArgsConstructor
public class SysMenuServiceImpl extends ServiceImpl<SysMenuMapper, SysMenu>
        implements SysMenuService {

    private final SysRoleMapper roleMapper;

    @Override
    public List<SysMenu> getMenuByAuth() {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        if (loginUser == null) {
            throw new ServiceException("loginUser is null，登陆失效");
        }
        if (loginUser.getPermissions().contains("*")) {
            return baseMapper.selectList(Wrappers.emptyWrapper());
        }
        MPJLambdaWrapper<SysRole> wrapper = new MPJLambdaWrapper<>();
        wrapper.selectAll(SysMenu.class)
                .leftJoin(SysRoleMenu.class, SysRoleMenu::getRoleId, SysRole::getRoleId)
                .leftJoin(SysMenu.class, SysMenu::getMenuId, SysRoleMenu::getMenuId)
                .in(SysRole::getRoleId, loginUser.getSysUser().getRoleIds());
        return roleMapper.selectJoinList(SysMenu.class, wrapper);
    }
}




